home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
INFO
/
PCCDEMO.ZIP
/
COMP1.EXE
/
BACKTRAK.PRS
< prev
next >
Wrap
Text File
|
1993-12-20
|
7KB
|
111 lines
üÇéèôæÇéèêìå ╬╠╬╠╬╠╬╠╧╧╬╠╡
│ │ │ . │ Backtracking is a very general
├┐┌┐┌┐│┌├ ┌┐┌┐┌┐│┌┬┌┐┌┐ │ technique that is used to solve a
││┌┤│ ├┤│ │ ┌┤│ ├┤│││││ │ wide variety of problems that
└┘└┘└┘└└└┘┴ └┘└┘└└┴└└└┤ │ exist in Computer Science,
└┘ │ especially in the field of
ßαΓΩ≤±αΓΩΦφµ Φ≥ εφΣ εσ ≤τΣ │ Artificial Intelligence (AI).
σ⌠φπα∞Σφ≤αδ ΣδΣ∞Σφ≤≥ εσ ∩±εδεµ, α │ This may be seen by the fact that
δαφµ⌠αµΣ Φφ ÷τΦΓτ ∞αφ√ Σ≈∩Σ±≤ │ the very languages Lisp, along
≥√≥≤Σ∞≥ αφπ φα≤⌠±αδ δαφµ⌠αµΣ │ with its many dialects such as
∩±εΓΣ≥≥Φφµ ≥√≥≤Σ∞≥ τα⌡Σ ßΣΣφ │ Scheme, and PROLOG comprise the
Φ∞∩δΣ∞Σφ≤Σπ. │ linga franca of Artificial
│ Intelligence and yet have at
│ their very core recursion and
│ backtracking.
ß√ πα⌡Φπ ∞. ÷ΦδδΦα∞≥ │
DipMin BCompSc AACS PCP │ As a general definition,
│ ßαΓΩ≤±αΓΩΦφµ Φ≥ ≤τΣ ≤ΣΓτφΦ≡⌠Σ εσ
│ µεΦφµ ßαΓΩ÷α±π≥ ε⌡Σ± εφΣ'≥ ≥≤Σ∩≥
│ Φφ ε±πΣ± ≤ε Σ≈∩δε±Σ α φΣ÷ ∩α≤τ,
│ ε± ∞Σ≤τεπ εσ ≥εδ⌡Φφµ ≤τΣ ∩±εßδΣ∞
α≤ ταφπ. This may continue until │ Minotaurs chasing you, a possible
either one solution is reached, │ technique is to stick to one wall
or every possible solution is │ and take every left turn (or
discovered, or even until every │ every right turn, as long as we
possible alternative has been │ are consistent throughout).
explored with none providing a │ However we wish to find every
solution. │ possible path through the maze,
│ and not merely stop with one.
As an example, one might like │ Therefore, from our starting
to consider a general algorithm │ position we wish to explore paths
for finding a path through a │ that lead in all four major
maze. That is, if we imagine that │ compass directions (provided a
we are trapped in the centre of a │ path exists in that direction).
maze, how is it possible to find │ From every possible position we
every path that leads back to the │ could be in along these paths, we
outside world? Well, if you just │ wish to explore paths that lead
happen to be Theseus you trail a │ in all four major compass
string behind you on the way in, │ directions also.
and then trace it back out again. │ This leads us to the simple
Of course, if there are no │ algorithm :
│ always searching a maze that has
To search a maze (from the │ been partially searched. The
current position) ... │ computer maintains a stack of
│ copies of the maze - one for each
mark the current position as part │ of the partially completed
of the way out; │ procedure calls. Each copy shows
│ the path that has led to that
if we're at the exit, print the │ particular location. If we happen
maze (with the path taken so far) │ to be at the exit of the maze,
│ then the current copy of the maze
else if we can, search a │ shows the way out.
maze (starting one step left); │
if we can, search a │ If we do not get to an exit,
maze (starting one step up); │ nothing happens at all. Suppose
if we can, search a │ we take a left turn into a dead
maze (starting one step right); │ end. Since we are unable to go
if we can, search a │ left, forward or right, that
maze (starting one step down); │ particular invocation of the
│ procedure ends, and its copy of
In this algorithm, we are │ the maze (with an incorrect path)
is removed. Thus, this serves as │ conflicting with previously
a good example of backtracking. │ placed queens then the current
│ chessboard is wrong and we
A second classic backtracking │ backtrack - that is, attempt to
problem is that of the "8 │ place the previously most recent
Queens", or more generally, the │ queen in its column again.
"n-Queens". In this situation, │ Otherwise, if we happen to
one must determine all ways to │ place a queen in the last column
place n non-taking queens on a n │ then we have succeeded. Following
by n chessboard. To solve this │ this article is an implementation
problem, one must begin with an │ in LISP of this problem.
empty chessboard and attempt to │
build up a solution column by │ Many other famous backtracking
column, beginning with the │ problems exist, such as the
leftmost. Again the computer │ "Knight's Tour".
maintains a stack of copies of │ Here a chessboard is again the
the chessboard with queens in │ scene, but this time one must
various positions. If we are │ take a Knight around the entire
unable to place a queen in a │ board such that it lands on every
column as it is always │ square, and each square only
once. Can a path be found? If one │ turns, regardless of how far one
uses backtracking techniques, the │ must revert in order to set foot
answer is a resounding 'Yes!'. │ in the correct direction again.
│
Of course, the applications of │ Backtracking is certainly a
backtracking are more than merely │ useful tool in the repetoire of
solving trivial little problems. │ the Computer Scientist and its
ßαΓΩ≤±αΓΩΦφµ Γε∞Σ≥ Φφ≤ε ∩δα√ ÷τΣφ │ use and ramifications should be
εφΣ ⌠≥Σ≥ αφ√ σε±∞ εσ ≥Σα±Γτ │ carefully investigated and
αδµε±Φ≤τ∞ ≥⌠Γτ α≥ πΣ∩≤τ-σΦ±≥≤, ε± │ understood ñ
ß±Σαπ≤τ-σΦ±≥≤. As has already │
been stated, backtracking is one │
of the fundamental elements of │
PROLOG, a language in which many │
expert systems and natural │
language processing systems have │
been implemented. Even compilers │
may take advantage of │
backtracking - in short, it │
allows one to recover from wrong │